package com.lippeng.taohua.db.util;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lippeng.taohua.db.domain.SuperDomain;
import com.lippeng.taohua.db.exception.TaoHuaDBException;

/**
 * This helper is created for SqlBuilder. I decompose domain here
 * 
 * @author lippeng
 * 
 */
public class SqlHelper {

	public static final String TYPE_STRING = "java.lang.String";
	public static final String TYPE_LONG = "java.lang.Long";
	public static final String TYPE_LONG2 = "long";
	public static final String TYPE_INTEGER = "java.lang.Integer";
	public static final String TYPE_INT = "java.lang.Integer";
	public static final String TYPE_DOUBLE = "java.lang.Double";
	public static final String TYPE_DOUBLE2 = "double";
	public static final String TYPE_FLOAT = "java.lang.Float";
	public static final String TYPE_FLOAT2 = "float";
	public static final String TYPE_DATE = "java.util.Date";

	private SuperDomain domain;
	private Class<? extends SuperDomain> classs;

	// save all field name
	private List<String> allFieldNameList = new ArrayList<String>();
	// save the unNull field name
	private List<String> fieldNameList = new ArrayList<String>();
	// save the unNull field Value
	private List<String> fieldValueList = new ArrayList<String>();
	private Field[] fiealds;

	/**
	 * Constructor
	 * 
	 * @param domain
	 */
	public SqlHelper(SuperDomain domain) {
		this.domain = domain;
		this.classs = domain.getClass();
		init();
	}

	/**
	 * if there is something in domain field,save this field into the List
	 */
	private void init() {
		fiealds = classs.getDeclaredFields();
		String fieldName = null;
		String fieldValue = null;
		String fieldType = null;
		for (Field field : fiealds) {
			fieldName = field.getName();

			fieldType = field.getType().getName();

			try {
				Object valueObject = field.get(domain);
				if (TYPE_STRING.equals(fieldType)) {
					if (null != valueObject
							&& Util.isNull(valueObject.toString().trim())) {
						fieldValue = "\"" + field.get(domain).toString() + "\"";
					} else {
						fieldValue = null;
					}
				} else if (TYPE_DATE.equals(fieldType)) {
					if (null != valueObject
							&& Util.isNull(valueObject.toString().trim())) {
						Date date = (Date) valueObject;
						SimpleDateFormat dateFormat = new SimpleDateFormat(
								"yyyy-MM-dd hh:mm:ss");
						fieldValue = dateFormat.format(date);
					} else {
						fieldValue = null;
					}
				} else {
					if (null != valueObject
							&& Util.isNull(valueObject.toString().trim())) {
						fieldValue = valueObject.toString();
					} else {
						fieldValue = null;
					}
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
				throw new TaoHuaDBException("Get Field Value Fail");
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				throw new TaoHuaDBException("Get Field Value Fail");
			}
			allFieldNameList.add(fieldName);
			if (null != fieldValue) {
				fieldNameList.add(fieldName);
				fieldValueList.add(fieldValue);
			}
		}
	}

	/**
	 * Methods
	 */

	/**
	 * Get the simple name of domain and this is the table name
	 */
	public String getTableName() {
		String value = classs.getSimpleName();
		return value;
	}

	/**
	 * Get columns name and join them with ","
	 * 
	 * @return
	 */
	public String getColumnsName() {
		String value = null;
		StringBuilder sBuilder = new StringBuilder();
		for (String string : fieldNameList) {
			sBuilder.append(string);
			sBuilder.append(",");
		}
		value = sBuilder.substring(0, sBuilder.length() - 1);
		return value;
	}

	/**
	 * Get columns value and join them with ","
	 * 
	 * @return
	 */
	public String getValuesName() {
		String value = null;
		StringBuilder sBuilder = new StringBuilder();
		for (String string : fieldValueList) {
			sBuilder.append(string);
			sBuilder.append(",");
		}
		value = sBuilder.substring(0, sBuilder.length() - 1);
		return value;
	}

	/**
	 * Get the id of domain
	 * 
	 * @return
	 */
	public String getId() {
		int index = fieldNameList.indexOf("id");
		if (index > -1) {
			return fieldValueList.get(index);
		} else {
			throw new TaoHuaDBException("The id field is null");
		}
	}

	/**
	 * Get the update conditions
	 * 
	 * @return
	 */
	public String getUpdateValue() {
		String value = null;
		StringBuilder sBuilder = new StringBuilder();
		for (int i = 0; i < fieldNameList.size(); i++) {
			sBuilder.append(fieldNameList.get(i));
			sBuilder.append("=");
			sBuilder.append(fieldValueList.get(i));
			sBuilder.append(",");
		}
		value = sBuilder.substring(0, sBuilder.length() - 1);
		return value;
	}

	/**
	 * Get the update conditions
	 * 
	 * @return
	 */
	public String getUpdateNullValue() {
		// TODO here is need to do ,but not now

		return null;
	}

	/**
	 * Get the select conditions
	 * 
	 * here is indistinct
	 * 
	 * @return
	 */
	public String getSelectCondition() {
		String value = null;
		StringBuilder sBuilder = new StringBuilder();
		for (int i = 0; i < fieldNameList.size(); i++) {
			sBuilder.append(fieldNameList.get(i));
			sBuilder.append(" like \'%");
			sBuilder.append(fieldValueList.get(i));
			sBuilder.append("%\'");
			sBuilder.append(",");
		}
		value = sBuilder.substring(0, sBuilder.length() - 1);
		return value;
	}

	/**
	 * Get the select conditions
	 * 
	 * here is distinct
	 * 
	 * @return
	 */
	public String getSelectEqualCondition() {
		String value = null;
		StringBuilder sBuilder = new StringBuilder();
		for (int i = 0; i < fieldNameList.size(); i++) {
			sBuilder.append(fieldNameList.get(i));
			sBuilder.append(" = ");
			sBuilder.append(fieldValueList.get(i));
			sBuilder.append(",");
		}
		value = sBuilder.substring(0, sBuilder.length() - 1);
		return value;
	}

	public Object getAllColumnNames() {
		String value = null;
		StringBuilder sBuilder = new StringBuilder();
		for (String fieldName : allFieldNameList) {
			sBuilder.append(fieldName);
			sBuilder.append(",");
		}
		value = sBuilder.substring(0, sBuilder.length() - 1);
		return value;
	}

}
